/**
 * Copyright (C) 2021-2022 maminjie <canpool@163.com>
 * SPDX-License-Identifier: LGPL-2.1
 * Powered by Qt-Advanced-Docking-System
**/

#include "docksplitter.h"

#include <QVariant>

#include "dockpannel.h"

QADS_BEGIN_NAMESPACE

class DockSplitterPrivate
{
public:
    DockSplitterPrivate(DockSplitter *parent)
        : q(parent)
    {}

private:
    DockSplitter *q;
};

DockSplitter::DockSplitter(QWidget *parent)
    : QSplitter(parent)
    , d(new DockSplitterPrivate(this))
{
    setAttribute(Qt::WA_StyledBackground, true);
    setProperty("DockSplitter", true);
    setChildrenCollapsible(false);
}

DockSplitter::DockSplitter(Qt::Orientation orientation, QWidget *parent)
    : QSplitter(orientation, parent)
    , d(new DockSplitterPrivate(this))
{}

DockSplitter::~DockSplitter()
{
    delete d;
}

bool DockSplitter::hasVisibleContent() const
{
    // TODO Cache or precalculate this to speed up
    for (int i = 0; i < count(); ++i) {
        if (!widget(i)->isHidden()) {
            return true;
        }
    }

    return false;
}

QWidget *DockSplitter::firstWidget() const
{
    return (count() > 0) ? widget(0) : nullptr;
}

QWidget *DockSplitter::lastWidget() const
{
    return (count() > 0) ? widget(count() - 1) : nullptr;
}

/**
 * Returns true if the splitter contains central widget of dock manager.
 */
bool DockSplitter::isResizingWithContainer() const
{
    for (auto pannel : findChildren<DockPannel *>()) {
        if (pannel->isCentralPannel()) {
            return true;
        }
    }

    return false;
}

QADS_END_NAMESPACE
